﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using InfoSoftGlobal;
using System.Drawing;

public partial class piechart : System.Web.UI.Page
{
    SQLInteract objSQL = new SQLInteract();

    protected void Page_Load(object sender, EventArgs e)
    {
        int pageopt = int.Parse(Request.QueryString["pt"].ToString().Trim());
        if (pageopt == 0)
        {
            bystatus();
        }
        else if (pageopt == 1)
        {
            byteam();
        }
        else if (pageopt == 2)
        {
            defect_summary();
        }
        else 
        {
            Response.Write("Error Occured.");
        }
    }

    public void bystatus()
    {
        //collect data from sql server
        string project_name = Request.QueryString["project"].ToString().Trim();
        string release_name = Request.QueryString["release"].ToString().Trim();
        //string system_name = Request.QueryString["system"].ToString().Trim();
        Page.Title = "Defect By Priority - " + project_name + "("+ release_name + ")";
        SqlDataReader sdr;
        sdr = objSQL.ReturnDataReader("select DefectID,Project,Status,Detected_In_Release,System_Detected_In,Priority from Defects where Detected_In_Release like '%" + release_name + "%' and Project like '%" + project_name + "%' order by Project");
        int count = 0;
        string projectList1 = "";

        int statusClosed = 0;
        string statusClosedList = "";
        Array statusClosedarray;

        int statusReopen = 0;
        string statusReopenList = "";
        Array statusReopenarray;

        int statusNew = 0;
        string statusNewList = "";
        Array statusNewarray;

        string projectList = "";

        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                //project array
                if (projectList1 == "" || sdr["Project"].ToString().Trim() != projectList1)
                {
                    projectList = projectList.Trim().ToString() + sdr["Project"].ToString().Trim() + ",";
                }

                if (sdr["Project"].ToString().Trim() != projectList1 && projectList1 != "")
                {
                    statusClosedList = statusClosedList + statusClosed.ToString().Trim() + ",";
                    statusReopenList = statusReopenList + statusReopen.ToString().Trim() + ",";
                    statusNewList = statusNewList + statusNew.ToString().Trim() + ",";
                    statusReopen = 0;
                    statusClosed = 0;
                    statusNew = 0;
                }

                if (sdr["Priority"].ToString().Trim() == "1 - Urgent")
                {
                    statusClosed = statusClosed + 1;
                }

                if (sdr["Priority"].ToString().Trim() == "2 - High")
                {
                    statusReopen = statusReopen + 1;
                }

                if (sdr["Priority"].ToString().Trim() == "3 - Low")
                {
                    statusNew = statusNew + 1;
                }

                projectList1 = sdr["Project"].ToString().Trim();
                count = count + 1;
            }
            statusClosedList = statusClosedList + statusClosed.ToString().Trim() + ",";
            statusReopenList = statusReopenList + statusReopen.ToString().Trim() + ",";
            statusNewList = statusNewList + statusNew.ToString().Trim() + ",";
        }
        statusClosedarray = statusClosedList.Split(new char[] { ',' });
        statusReopenarray = statusReopenList.Split(new char[] { ',' });
        statusNewarray = statusNewList.Split(new char[] { ',' });

        if (statusClosedarray.GetUpperBound(0) == 1 || statusReopenarray.GetUpperBound(0) == 1 || statusNewarray.GetUpperBound(0) == 1)
        {
            string strXML = "";
            strXML = "<graph showNames='1' decimalPrecision='0' caption='Defect By Priority' subCaption='" + project_name + "(" + release_name + ")'>";
            strXML = strXML + "<set name='Urgent' color='9D080D' value='" + statusClosedarray.GetValue(0) + "' />";
            strXML = strXML + "<set name='High' color='F6BD0F' value='" + statusReopenarray.GetValue(0) + "' />";
            strXML = strXML + "<set name='Low' color='588526' value='" + statusNewarray.GetValue(0) + "' />";
            strXML = strXML + "</graph>";
            fcLiteral.Text = FusionCharts.RenderChart("FusionCharts/FCF_Pie3D.swf", "", strXML, "productSales", "780", "450", false, false);
        }
        else
        {
            Response.Write("<br/><br/><br/><br/><br/><center><b>No defect with status as Open</center></b><br/><br/><br/><br/><br/>");
        }
    }

    public void byteam()
    {
        //collect data from sql server
        string project_name = Request.QueryString["project"].ToString().Trim();
        string release_name = Request.QueryString["release"].ToString().Trim();
        //string system_name = Request.QueryString["system"].ToString().Trim();
        Page.Title = "Defect By Priority - " + project_name + "("+ release_name + ")";
        SqlDataReader sdr;
        sdr = objSQL.ReturnDataReader("select count(*) as num, assigntoteam from Defects where Detected_In_Release like '%" + release_name + "%' and Project like '%" + project_name + "%' and Status <> 'Closed' group by assigntoteam");
        int count = 0;
        string[] colors = new string[10] { Color.Red.ToString(), Color.Blue.ToString(), Color.LightYellow.ToString(), Color.Honeydew.ToString(), Color.Olive.ToString(), Color.Peru.ToString(), Color.SeaGreen.ToString(), Color.SkyBlue.ToString(), Color.Wheat.ToString(), Color.Tomato.ToString() }; 
        Array projectarray;
        Array statusarray;
        string statusFixedList = "";

        string projectList = "";
        string assigntoteam = "";
        string setXML="";

        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                //project array
                assigntoteam = sdr["assigntoteam"].ToString().Trim();
                setXML = setXML + "<set name='" + assigntoteam + "' color='"+ colors[count] +"' value='" + sdr["num"] + "' />";
                projectList = projectList + sdr["assigntoteam"] + ",";
                statusFixedList = statusFixedList + sdr["num"] + ",";
                count = count + 1;
            }
        }
        projectarray = projectList.Split(new char[] { ',' });
        statusarray = statusFixedList.Split(new char[] { ',' });

        if (setXML !="")
        {
            string strXML = "";
            strXML = "<graph showNames='1' decimalPrecision='0' caption='Defect By Team' subCaption='" + project_name + "(" + release_name + ")'>";
            strXML = strXML + setXML;
            strXML = strXML + "</graph>";
            fcLiteral.Text = FusionCharts.RenderChart("FusionCharts/FCF_Pie3D.swf", "", strXML, "productSales", "780", "450", false, false);
        }
        else
        {
            Response.Write("<br/><br/><br/><br/><br/><center><b>No defect with status as Open</b></center><br/><br/><br/><br/><br/>");
        }
    }

    public void defect_summary()
    {
        //collect data from sql server
        string project_name = Request.QueryString["project"].ToString().Trim();
        string release_name = Request.QueryString["release"].ToString().Trim();
        //string system_name = Request.QueryString["system"].ToString().Trim();
        Page.Title = "Defect By Priority - " + project_name + "("+ release_name + ")";
        SqlDataReader sdr;
        sdr = objSQL.ReturnDataReader("select DefectID,Project,Status,Detected_In_Release,System_Detected_In,assigntoteam from Defects where Detected_In_Release like '%" + release_name + "%' and Project = '" + project_name + "'  and Status <> 'Closed' order by assigntoteam");
        int count = 0;
        string projectList1 = "";
        Array projectarray;
        int statusClosed = 0;
        string statusClosedList = "";
        Array statusClosedarray;

        int statusReopen = 0;
        string statusReopenList = "";
        Array statusReopenarray;

        int statusOpen = 0;
        string statusOpenList = "";
        Array statusOpenarray;

        int statusNew = 0;
        string statusNewList = "";
        Array statusNewarray;

        int statusFixed = 0;
        string statusFixedList = "";
        Array statusFixedarray;

        string projectList = "";
        int total_defects=0;
        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                //project array
                if (projectList1 == "" || sdr["assigntoteam"].ToString().Trim() != projectList1)
                {
                    projectList = projectList.Trim().ToString() + sdr["assigntoteam"].ToString().Trim() + ",";
                }

                if (sdr["Status"].ToString().Trim() == "Closed")
                {
                    statusClosed = statusClosed + 1;
                }

                if (sdr["Status"].ToString().Trim() == "Reopen")
                {
                    statusReopen = statusReopen + 1;
                }

                if (sdr["Status"].ToString().Trim() == "New")
                {
                    statusNew = statusNew + 1;
                }

                if (sdr["Status"].ToString().Trim() == "Open")
                {
                    statusOpen = statusOpen + 1;
                }

                if (sdr["Status"].ToString().Trim() == "Fixed")
                {
                    statusFixed = statusFixed + 1;
                }

                if (sdr["assigntoteam"].ToString().Trim() != projectList1 && projectList1 != "")
                {
                    statusClosedList = statusClosedList + statusClosed.ToString().Trim() + ",";
                    statusReopenList = statusReopenList + statusReopen.ToString().Trim() + ",";
                    statusOpenList = statusOpenList + statusOpen.ToString().Trim() + ",";
                    statusNewList = statusNewList + statusNew.ToString().Trim() + ",";
                    statusFixedList = statusFixedList + statusFixed.ToString().Trim() + ",";
                    statusReopen = 0;
                    statusOpen = 0;
                    statusClosed = 0;
                    statusNew = 0;
                    statusFixed = 0;
                }

                projectList1 = sdr["assigntoteam"].ToString().Trim();
                count = count + 1;
            }
            statusClosedList = statusClosedList + statusClosed.ToString().Trim()+",";
            statusReopenList = statusReopenList + statusReopen.ToString().Trim()+",";
            statusOpenList = statusOpenList + statusOpen.ToString().Trim() + ",";
            statusNewList = statusNewList + statusNew.ToString().Trim()+",";
            statusFixedList = statusFixedList + statusFixed.ToString().Trim()+",";
        }
        projectarray=projectList.Split(new char[] { ',' });
        statusClosedarray = statusClosedList.Split(new char[] { ',' });
        statusFixedarray = statusFixedList.Split(new char[] { ',' });
        statusOpenarray = statusOpenList.Split(new char[] { ',' });
        statusReopenarray = statusReopenList.Split(new char[] { ',' });
        statusNewarray = statusNewList.Split(new char[] { ',' });

        string tbl = "<br/><br/><br/><br/><center><b>"  + project_name + "(" + release_name + ")</b><br/><br/><table width='99%' border=1 cellspacing=1 cellpadding=1>";
        tbl += "<tr><th><b><font>Assign To Team</font></b></th><th><b>Reopen</b></th><th><b>New</b></th><th><b>Fixed</b></th><th><b>Open</b></th><th><b>Total</b></th></tr>";
        if (projectarray.GetUpperBound(0) > 0)
        {
            total_defects = (int.Parse(statusNewarray.GetValue(0).ToString()) + int.Parse(statusReopenarray.GetValue(0).ToString()) + int.Parse(statusFixedarray.GetValue(0).ToString()) + int.Parse(statusOpenarray.GetValue(0).ToString()));
            string aa="";
            total_defects=0;
            
            string assign_team="";
            foreach(string item in projectarray)
            {
            if (item != "") 
                {
                    assign_team=item;
                    aa= aa + "<tr><td>" + assign_team + "</td></tr>";
                }
            }

            string reopen="";
            foreach(string item in statusReopenarray)
            {
                if(item != "" ){ 
                    total_defects=(total_defects + (int.Parse(item)));
                    reopen= reopen + "<tr><td>" + item + "</td></tr>";
                }
            }


            string new1="";
            foreach(string item in statusNewarray)
            {
                if (item != "" )
                { 
                    total_defects=(total_defects + (int.Parse(item)));
                    new1 = new1 + "<tr><td>" + item + "</td></tr>";
                }
            }


            string fix="";
            foreach(string item in statusFixedarray)
            {
                if (item != "" ){ 
                    total_defects=(total_defects + (int.Parse(item)));
                    fix = fix + "<tr><td>" + item + "</td></tr>";
                }
            }

            string open="";
            foreach(string item in statusOpenarray)
            {
                if(item != "" )
                { 
                    total_defects=(total_defects + (int.Parse(item)));
                    open= open + "<tr><td>" + item + "</td></tr>";
                }
            }
            
            tbl += "<tr><td><table>"+ aa +"</table></td><td><table>"+reopen+"</table></td><td><table>"+new1+"</table></td><td><table>"+fix+"</table></td><td><table>"+open+"</table></td><td>"+total_defects+"</td></tr></table></center>";
            fcLiteral.Text = tbl;
        }
        else
        {
            Response.Write("<br/><br/><br/><br/><br/><center><b>No defect with open status found!!!</b></center><br/><br/><br/><br/><br/>");
        }
    }

}



